package yl.hs.bmipfull.service.laboratory.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratory.IProductConfigDao;
import yl.hs.bmipfull.pojo.laboratory.ProductConfigDetailsEntity;
import yl.hs.bmipfull.pojo.laboratory.ProductConfigEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialInfoEntity;
import yl.hs.bmipfull.service.laboratory.IProductConfigDetailsService;
import yl.hs.bmipfull.service.laboratory.IProductConfigService;
import yl.hs.bmipfull.service.material.IRawMaterialInfoService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 作用:  产品配比表服务层接口实现<br/>
 * 创建时间: 2021年05月25日 11时24分32秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ProductConfigServiceImpl extends LaboratoryBaseService implements IProductConfigService {

    @Autowired
    IProductConfigDao dao;
    @Autowired
    IRawMaterialInfoService rawService;

    @Autowired
    IProductConfigDetailsService detailsService;

    @Override
    public List<ProductConfigEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductConfigEntity.class);
        List<ProductConfigEntity> configs = dao.queryList(pageQuery.getQuery());
        for (ProductConfigEntity config : configs) {
            ProductConfigEntity bySN = dao.findBySN(config.getTheoreticalSn());
            if (bySN != null) {
                config.setTheoreticalName(bySN.getConfigName());
            }
        }
        return configs;
    }

    @Override
    public PageResult<ProductConfigEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductConfigEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<ProductConfigEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        List<ProductConfigEntity> configs = dao.queryPage(pageQuery.getQuery());
        for (ProductConfigEntity config : configs) {
            ProductConfigEntity bySN = dao.findBySN(config.getTheoreticalSn());
            if (bySN != null) {
                config.setTheoreticalName(bySN.getConfigName());
            }
        }
        result.setData(configs);
        return result;
    }

    @Override
    public List<ProductConfigEntity> queryListByTargetSN(String targetSN, Integer useYear, Integer useQuarter) {
        return dao.queryListByTargetSN(targetSN, useYear, useQuarter);
    }


    @Override
    public ProductConfigEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public Integer saveProductConfig(Map<String, Object> model, String sn, List<ProductConfigDetailsEntity> dates) throws Exception {
        Integer result = 0;
        ProductConfigEntity entity = new ProductConfigEntity();

        if (Tl.isEmpty(sn)) {
            //执行保存

            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProductConfigEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                insertConfigDetails(dates, entity.getSn());
                writeLog(ActionType.CREATE, entity.getSn(), "插入产品配比表信息", Tl.toJson(entity), ProductConfigEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                insertConfigDetails(dates, entity.getSn());
                writeLog(ActionType.UPDATE, sn, "更新产品配比表信息", Tl.toJson(entity), ProductConfigEntity.class);
            }
        }

        return result;
    }

    /**
     * 添加配比明细信息
     *
     * @param dates    配比明细信息
     * @param configSn 配比编号
     * @return
     */
    public Integer insertConfigDetails(List<ProductConfigDetailsEntity> dates, String configSn) {
        Integer result = 0;
        if (dates != null && dates.size() > 0 && dates.get(0) != null) {
            try {
                for (ProductConfigDetailsEntity entity : dates) {
                    RawMaterialInfoEntity rawEntity = rawService.findBySn(entity.getRmSN());
                    if (rawEntity != null) {
                        entity.setRmBrand(rawEntity.getBrand());
                        entity.setRmName(rawEntity.getRmName());
                        entity.setRmSpec(rawEntity.getRmSpec());
                        entity.setRmCate(rawEntity.getRmCateSN());
                    }
                    if (entity.getRate() == null) {
                        entity.setRate(new BigDecimal(0));
                    }
                    entity.setConfigSN(configSn);
                }
                result = detailsService.saveProductConfigDetails(dates, configSn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public Integer delete(String[] sn) {
        var result = 0;
        if (sn.length > 0) {
            result = dao.changeStateBySn(sn, DataState.DELETED.code);
            if (result > 0) {
                writeLog(ActionType.DELETE, sn, ProductConfigEntity.class);
            }
        }
        return result;
    }

    @Override
    public Integer changeStateByYearAndQuarter(int useYear, int useQuarter, int configType, String targetSN, short state) {
        return dao.changeStateByYearAndQuarter(useYear, useQuarter, configType, targetSN, state);
    }

    @Override
    public Integer changesByYear(int useYear, int useQuarter, short state) {
        return dao.changesByYear(useYear, useQuarter, state);
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result=dao.changeStateBySn(snArray,state);
        if (result > 0) {
            forWriteLog(snArray,state);
        }
        return result;
    }
}